package com.irdstudio.efp.esb.api.rest.wsd;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.esb.api.bean.wsd.credit.req.finalcredit.NlsWsdFinalReqBean;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 需求编号:2019D0910
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/8/12
 * 功能描述:
 * 修改日期:2019/8/12
 * 修改描述:
 */
@Controller
@RequestMapping("esb")
public class NlsWsdApprController {

    private static Logger logger = LoggerFactory.getLogger(NlsWsdApprController.class);
    // 授信初审标识
    private static final String FINST_APPR = "FIRST";
    // 授信终审标识
    private static final String FINAL_APPR = "FINAL";

    private static final Map<String, String> forwardMap = new HashMap<>();

    static{
        forwardMap.put("0",FINST_APPR);
        forwardMap.put("1",FINAL_APPR);
    }

    @PostMapping("/SERVICE_30220001_07")
    public String doRecv(HttpServletRequest request) throws Exception {
        logger.info("======>网商贷授信接口【30220001_07】开始<======");

        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        // 服务代码
        String svcCd = esbReqServiceBean.getSysHead().getSvcCd();
        // 场景
        String scnCd = esbReqServiceBean.getSysHead().getScnCd();

        String esbReqBody = (String) request.getAttribute("esbReqBody");
        NlsWsdFinalReqBean wsdReqBean = JSONObject.parseObject(esbReqBody, NlsWsdFinalReqBean.class);
        String forwardCode = "";
        String approvFlag = forwardMap.get(wsdReqBean.getTrlRvwFlg());
        if (wsdReqBean != null){
            forwardCode = svcCd+"_"+scnCd+"_"+approvFlag;
        }
        if (StringUtils.isBlank(approvFlag)) {
            forwardCode = svcCd+"_"+scnCd+"_DEFAULT";
        }
        return "forward:/esb/" + forwardCode;
    }
}
